package com.link2room.bumblebee.facade.impl;

import com.link2room.bumblebee.dto.param.base.PmsReservationOptionParamDto;
import com.link2room.bumblebee.dto.result.base.PmsReservationOptionDto;
import com.link2room.bumblebee.exception.BumblebeeException;
import com.link2room.bumblebee.facade.IPmsReservationOptionFacadeService;
import com.link2room.bumblebee.util.BumblebeeUtil;
import com.link2room.common.dto.param.base.BaseParamDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.redis.RedisUtil;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.JacksonUtil;
import com.link2room.common.util.StringUtil;
import com.link2room.bumblebee.entity.PmsReservationOption;
import com.link2room.enums.exception.BumblebeeSysExceptionEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by sss on 2017/5/15.
 */
@Component("pmsReservationOptionFacadeService")
public class PmsReservationOptionFacadeServiceImpl implements IPmsReservationOptionFacadeService {
    @Resource
    private ICommonService commonService;
    @Resource
    private RedisUtil redisUtil;

    @Override
    public QueryResult<PmsReservationOptionDto> listPmsReservationOptions(BaseParamDto baseParamDto)throws Exception{
        PmsReservationOption queryPRO=new PmsReservationOption();
        queryPRO.setLhotelGroupCode(baseParamDto.getLhotelGroupCode());
        QueryResult<PmsReservationOption> pmsReservationOptionQueryResult=commonService.findAllEQ(PmsReservationOption.class,queryPRO);
        QueryResult<PmsReservationOptionDto> pmsReservationOptionDtoQueryResult=new QueryResult<>();
        List<PmsReservationOptionDto> targetList=new ArrayList<>();
        if(pmsReservationOptionQueryResult.getResultlist()!=null){
            for(PmsReservationOption source:pmsReservationOptionQueryResult.getResultlist()){
                PmsReservationOptionDto target=ClassConverUtil.copyPropertiesToDto(source,PmsReservationOptionDto.class, false);
                if(StringUtil.isNotBlank(source.getTaCode())){
                    target.setTaCodeMap(JacksonUtil.jsonToBean(source.getTaCode(), Map.class));
                }
                targetList.add(target);
            }
        }
        pmsReservationOptionDtoQueryResult.setResultlist(targetList);
        pmsReservationOptionDtoQueryResult.setTotalrecord(pmsReservationOptionQueryResult.getTotalrecord());
        pmsReservationOptionDtoQueryResult.setCurrentPage(pmsReservationOptionQueryResult.getCurrentPage());
        return pmsReservationOptionDtoQueryResult;
    }

    @Override
    public PmsReservationOptionDto getPmsReservationOptionDto(PmsReservationOptionParamDto pmsReservationOptionParamDto)throws Exception{
        String lhotelGroupCode=pmsReservationOptionParamDto.getLhotelGroupCode();
        String pbChannel=pmsReservationOptionParamDto.getPbChannel();
        String resType=pmsReservationOptionParamDto.getResType();
        PmsReservationOptionDto pmsReservationOptionDto= (PmsReservationOptionDto) redisUtil.get(
                BumblebeeUtil.getPmsReservationOptionKey(lhotelGroupCode, pbChannel,resType)
        );
        if(pmsReservationOptionDto==null){
            PmsReservationOption queryPRO=new PmsReservationOption();
            queryPRO.setLhotelGroupCode(lhotelGroupCode);
            queryPRO.setPbChannel(pbChannel);
            queryPRO.setResType(resType);
            PmsReservationOption pmsReservationOption=commonService.findOneEQ(PmsReservationOption.class,queryPRO);
            if(pmsReservationOption!=null){
                pmsReservationOptionDto=ClassConverUtil.copyPropertiesToDto(pmsReservationOption,PmsReservationOptionDto.class,false);
            }else {
                throw new BumblebeeException(BumblebeeSysExceptionEnum.NO_PMSRESERVATIONOPTION);
            }
        }
            return pmsReservationOptionDto;
        }
    }
